import React, { Component } from 'react';
import { message } from 'antd';
import _ from 'lodash';
import TitleItem from '../../components/TitleItem';
import HandleMemberSelect from '../common/handleMemberSelect';
import MemberList, { permissionList } from '../../components/MemberList';

const styles = {
    chat: {
        fontSize: '24px',
        cursor: 'pointer'
    },
}

const opts = {
    setHost: 'ishost',
    setManager: 'ismanager'
}


class TaskMember extends Component {

    constructor(props) {
        super(props);
        this.state = {
            showModal: false,
            isAdd: false,
            members: []
        }

    }

    componentWillMount() {
        this.handelSetValue(this.props);
    }

    componentWillReceiveProps(nextProps) {
        this.handelSetValue(nextProps);
    }

    handelSetValue = (props) => {
        const { create, info, detailData } = props;
        if (create) { // 设置当前账号为默认成员
            this.setBaseValue({
                members: [{
                    ...info,
                    ismanager: true, ishost: true, order: 1,
                    username: info.realname,
                    userid: window.localStorage.getItem('userId'),
                    recid: ''
                }]
            });
            return;
        };
        this.setBaseValue(detailData);
    }

    setBaseValue = ({ members = [] }) => {
        this.setState({ members });
    }

    getValue = () => {
        const { members } = this.state;
        const list = members.map((v, i) => ({
            recid: v.recid, userid: v.userid, username: v.username,
            ismanager: v.ismanager || false,
            ishost: v.ishost || false,
            order: i + 1
        }));
        return list
    }

    handleMember = ({ domEvent, key }, v) => {
        domEvent.stopPropagation();
        domEvent.preventDefault();

        const { create } = this.props;
        const { members } = this.state;

        // 转移权限的限制处理
        // 限制创建项目的时候同时把权限都转移给其他人
        if (create) {
            const currentUser = members[0];
            if (opts[key] === 'ishost' && !currentUser.ismanager) { // 要设置其它人为主办人
                message.warn('创建阶段不可以把主办人和管理员权限都转移给其人人员');
                return;
            } else if (opts[key] === 'ismanager' && !currentUser.ishost) { // 要设置其它人为管理人
                message.warn('创建阶段不可以把主办人和管理员权限都转移给其人人员');
                return;
            }
        }

        const { manageMember, detailData } = this.props;
        const memberid = v.userid;
        // 创建任务
        if (create) {
            const { members } = this.state;
            if (key === 'setManager') {
                const list = members.map(v => {
                    if (v.userid === memberid) return { ...v, ismanager: true }
                    else return { ...v, ismanager: false }
                })
                this.setState({ members: list });
            } else if (key === 'setHost') {
                const list = members.map(v => {
                    if (v.userid === memberid) return { ...v, ishost: true }
                    else return { ...v, ishost: false }
                })
                this.setState({ members: list });
            }
        } else { // 任务详情编辑
            const taskid = detailData && detailData.info.recid;
            manageMember(key, { taskid, memberid });
        }
    }

    onOk = ({ targets: arrs, targetsIds: arrIds }, callback) => {
        const { isAdd } = this.state;
        const { create } = this.props;
        // 任务详情编辑
        if (!create) {
            this.setState({ loading: true });
            const { manageMember, detailData } = this.props;
            const funKey = isAdd ? 'addMember' : 'deleteMember';
            const parmas = { taskid: detailData.info.recid };
            isAdd ? parmas.members = arrIds : parmas.memberid = arrIds[0];
            this.setState({ loading: true });
            manageMember(funKey, parmas, () => {
                this.setState({ loading: false });
                callback();
            })
        } else { // 新建任务的时候
            const { members } = this.state;
            let newMembers = [];
            if (isAdd) newMembers = [...members, ...arrs];
            else newMembers = members.filter(v => !arrs.some(s => s.userid === v.userid));
            callback();
            this.setState({ members: newMembers });
        }
    }

    goPage = () => {
        // console.log(this.props);
        const { detailData } = this.props;
        if (!detailData.info.chatgroupid) {
            message.warn('该聊天组未创建');
            return;
        }
        const { ipcRenderer } = window.electron;
        ipcRenderer.send('set-goPage', { index: 0, data: detailData.info });
    }


    render() {
        const { showModal, isAdd, members } = this.state;
        const { create, detailData, isShowInSlider } = this.props;

        const isFinish = _.get(detailData, 'info.taskstatus') === 2;
        let showList = [...permissionList];
        let hasBtn = create;

        if (!create) {
            const currentHost = _.get(detailData, 'info.ishost');
            const currentManager = _.get(detailData, 'info.ismanager');
            if (!currentHost) showList = showList.slice(1);
            if (!currentManager) showList = showList.slice(0, showList.length - 1);

            hasBtn = currentManager || currentHost;
        }

        return (
            <>
                <TitleItem
                    title="任务成员"
                    icon={!create && !isShowInSlider && <span className="mdi mdi-chat-processing mainColor" style={styles.chat} onClick={this.goPage} />}
                    require={create}
                />
                <MemberList
                    showList={showList}
                    members={members}
                    hasBtn={hasBtn && !isFinish}
                    handleMember={this.handleMember}
                    onAdd={() => this.setState({ showModal: true, isAdd: true })}
                    onMinus={() => this.setState({ showModal: true, isAdd: false })}
                />
                <HandleMemberSelect
                    title={'成员'}
                    create={create}
                    isAdd={isAdd}
                    loading={this.state.loading}
                    members={members}
                    onOk={this.onOk}
                    show={showModal}
                    onCancel={() => this.setState({ showModal: false })}
                />
            </>
        );
    }
}

export default TaskMember